/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package unilearn.dao.hibernate;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import unilearn.dao.GroupMemberDAO;
import unilearn.vo.GroupMember;

/**
 *
 * @author Yat
 */
public class GroupMemberDAOImpl extends HibernateDAO implements GroupMemberDAO {

    public GroupMemberDAOImpl() {
        super();
    }

    public GroupMemberDAOImpl(SessionFactory sessionFactory) {
        super(sessionFactory);
    }

    @Override
    public void setSessionFactory(SessionFactory sessionFactory) {
        super.setSessionFactory(sessionFactory);
    }

    @Override
    public List<GroupMember> getAll() throws Exception {
        return super.getAll(GroupMember.class.getName());
    }

    @Override
    public GroupMember get(int id) throws Exception {
        Session s = sessionFactory.openSession();
        Transaction t = s.beginTransaction();

        try {
            Query query = s.createQuery("from " + GroupMember.class.getName() + " gm where gm.id = " + id);
            List<GroupMember> gms = query.list();
            if ((gms == null) || (gms.isEmpty())) {
                return null;
            }

            return gms.get(0);
        } finally {
            t.commit();
            s.close();
        }
    }

    @Override
    public List<GroupMember> find(GroupMember groupMember) throws Exception {
        return (List<GroupMember>) super.find(groupMember);
    }

    @Override
    public GroupMember insert(GroupMember groupMember) throws Exception {
        super.insert(groupMember);
        return groupMember;
    }

    @Override
    public GroupMember update(GroupMember groupMember) throws Exception {
        super.update(groupMember);
        return groupMember;
    }

    @Override
    public void delete(GroupMember groupMember) throws Exception {
        super.delete(groupMember);
    }
}
